package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
@SessionAttributes({"account","passwordreset"})
public class Admin_AccountController {
    @Autowired
    private AccountService accountService;
    //管理员查看用户列表
    @GetMapping("/listAccounts")
    public String listAccounts(Model model){
        //获取用户列表
        List<Account> accountList = accountService.getAccountList();

        //System.out.print(accountList);
        //添加到model中
        model.addAttribute("accountList",accountList);

        return "admin/ListAllAccounts";
    }
    //管理员点击用户名来查看和修改用户信息
    @GetMapping("/editAccountForm")
    public String editAccountForm(String username, Model model){
        Account account = accountService.getAccount(username);
        model.addAttribute("account",account);
        return "admin/EditAccount";
    }
    //管理员重置用户密码，修改用户信息
    @RequestMapping("/editAccount")
    public String editAccount(Account account,String repeatedPassword,Model model){
        if (account.getPassword() == null || account.getPassword().length() == 0 || repeatedPassword == null || repeatedPassword.length() == 0) {
            String msg = "密码不能为空";
            model.addAttribute("msg", msg);
            return "admin/EditAccount";
        } else if (!account.getPassword().equals(repeatedPassword)) {
            String msg = "两次密码不一致";
            model.addAttribute("msg", msg);
            return "admin/EditAccount";
        } else {
            accountService.updateAccount(account);
            account = accountService.getAccount(account.getUsername());
            model.addAttribute("account", account);

            return "redirect:/admin/listAccounts";
        }
    }
    //重置密码
    @GetMapping("/resetPassword")
    public String resetPassword(Model model){
        return"admin/ResetPassword";
    }
    @GetMapping("/saveNewPassword")
    public String saveNewPassword(HttpSession session,String username,String password,String repeatedPassword, Model model){
        //由于这里的account是未登录状态，因此account.getPassword()所获取的值为null,因此account的password是否为null不作为判断条件
        Account account = accountService.getAccount(username);
        if(account == null){
            System.out.println("该用户不存在");
            String msg = "该用户不存在";
            model.addAttribute("msg",msg);
            return "admin/ResetPassword";
        }
        else if (repeatedPassword == null || repeatedPassword.length() == 0) {
            System.out.println(account.getPassword());
            System.out.println(repeatedPassword);
            System.out.println("密码不能为空");
            String msg = "密码不能为空";
            model.addAttribute("msg", msg);
            return "admin/ResetPassword";
        }else if (!password.equals(repeatedPassword)) {
            System.out.println("两次密码不一致");
            String msg = "两次密码不一致";
            model.addAttribute("msg", msg);
            return "admin/ResetPassword";
        } else {
            //设置该账户的密码
            account.setPassword(password);
            //在数据库中update
            accountService.updateAccount(account);
            account = accountService.getAccount(account.getUsername());
            model.addAttribute("account", account);

            return "redirect:/admin_catalog/admin_view";
        }

    }


}
